import member from '@/components/selectMember/selectMember.vue';
import {getUrlParam,dealString} from '@/static/js/filter.js'
export default {
	components:{member},
	data () {
		return {
			userName: '',
			dialog: false,
			implementationSchemeList: [],
			searchContent: '', //会员输入框
			examinationTypeArray:['收集资料','健康宣教','定期随访','提醒服务','具体指导'],
			nowDte: new Date().toISOString().slice(0, 10),
			isChoose:true
		}
	},
	mounted(){
		const editId = getUrlParam('id')
		if(editId){
			const _this = this;
			_this.isChoose = false
			_this.editId = editId;
			_this.$api.InterventionPlanDetails(editId).then((res)=>{
				_this.implementationSchemeList = res.data.result.appHealthInterventionPlanEntities;
				_this.implementationSchemeList.specificMatters = _this.implementationSchemeList.map((a,ai)=>{
					const DOM = '#editor'+ai
					setTimeout(()=>{uni.createSelectorQuery().select(DOM).context((c) => {
						this.editorCtx = c.context; this.editorCtx.setContents({html: a.specificMatters || ''})
						}).exec()},200)
				})
				_this.userId = res.data.result.userId
				_this.userName = res.data.result.userName
			})
		}
	},
	methods: {
		dealString,
		hidedialog() {
			this.dialog = false
		},
		// 具体事项输入
		editorInput2(e){
			this.editData[this.editIndex].specificMatters = e.detail.html	
		},
		chooseUser(e) {
			this.userId = e.userId;
			this.userName = e.userName;
			this.phone = e.phone;
			// this.statusQuestion();
		},
		bindDateChange(e,item){
			item.createdDate = e.detail.value
		},
		// 查看是否有未签发的问卷
		// statusQuestion() {
		// 	this.$api.findByStatusFindByStatus(this.userId).then((res) => {
		// 		if(res.data.returnCode == '506') {
		// 			uni.showToast({
		// 				title: res.data.message,
		// 				icon: 'none'
		// 			})
		// 			setTimeout(() => {
		// 				uni.navigateTo({
		// 					url:'/pages/quickly/intervenscheme/list'
		// 				})
		// 			},1000)
		// 		}
		// 	})
		// },
		addData() {  //点击添加
			const _this = this;
			const userInfo = JSON.parse(localStorage.userInfo)
			this.implementationSchemeList.push({
				createdDate: _this.nowDte,
				content:'',
				userId:_this.userId,
				userName:_this.userName,
				healthId: userInfo.id
			})
		},
		delThis3(index) {
			this.implementationSchemeList.splice(index, 1);
		},
		examinationType(e) {
			this.implementationSchemeList[this.pickerIndex].content = this.examinationTypeArray[e.detail.value]
			this.examinationTypeIndex = e.target.value;
			this.examinationTypeArrayType=this.examinationTypeArray[this.examinationTypeIndex]
		},
		showModalbtn(e) {
			if(!this.userId){
				uni.showToast({
					title:'请先选择会员！',icon:'none'
				})
				return;
			}
			if(this.implementationSchemeList.length <= 0){
				uni.showToast({
					title:'至少添加一条计划',icon:'none'
				})
				return;
			}
			if(this.editId){
				this.editSave(this.implementationSchemeList,e)
			}else{
				this.defaultsave(this.implementationSchemeList,e)
			}
			// this.defaultsave()
		},
		// 普通保存
		defaultsave(data,status){
			delete data.specificMatters;
			this.$api.InterventionPlanSave({appHealthInterventionPlanEntities:data,status:status,realName:this.userName,userId:this.userId}).then((res)=>{
				if(res.data.returnCode == 200){
					uni.showToast({
						title:  status==1?'保存成功':'签发成功' ,
						icon: 'none'						
					})
					setTimeout(() => {
						uni.navigateTo({
							url:'/pages/quickly/intervenscheme/list'
						})
					},1000)
				}else if(res.data.returnCode == 506) {
					uni.showToast({
						title: res.data.message,
						icon: 'none'
					})
				}
			})
		},
		editSave(data,status){
			delete data.specificMatters;
			data.map((a) => {
				delete a.id
			})
			this.$api.InterventionPlanUpdate({appHealthInterventionPlanEntities:data,status:status,realName:this.userName,userId:this.userId,id:this.editId}).then((res)=>{
				if(res.data.returnCode == 200){
					uni.showToast({
						title:  status==1?'修改成功':'签发成功' ,
						icon: 'none'						
					})
					setTimeout(() => {
						uni.navigateTo({
							url:'/pages/quickly/intervenscheme/list'
						})
					},1000)
				}else if(res.data.returnCode == 506) {
					uni.showToast({
						title: res.data.message,
						icon: 'none'
					})
				}
			})
		}
	},
}